<?php
/**
 * 培训管理
 * 
 * @author  Lee <a605333742@gmail.com>
 * @time    2016-07-13 
 */
namespace Home\Controller;
use \Org\Net\Http;

class TrainController extends AllowController{
    /*
     * 构造函数
     *
     * @return  #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db       =D('TrainMod');
        $this->db_data  =D('Data');
    }
    
    /*
     * 资料列表
     * 
     * @return #
     */
    public function index(){
        
        //部门列表 start
        $secList    =D("Section")->where(array("status"=>1))->select(array("field"=>'id,logo,name'));
        
        //部门列表 end
        //查询模块个数
        $where['status']=1;
        $num            =$this->db->where($where)->count();
        if($num%2==1){
            $num    =$num+1;
        }
        //左边显示一半
        $leftMod    =$this->db->where($where)->order('id asc')->limit(0,$num/2)->select();
        foreach($leftMod as $k=>$v){
            $leftMod[$k]['desc_']   = explode(",",$leftMod[$k]['desc']);
        }        
        $leIds      =array();        
        foreach($leftMod as $v1){
            $leIds[]=$v1['id'];
        }
        $where1['t_id'] =array("in",  implode(",", $leIds));  
        
        $leDataList     =$this->db_data->where($where1)->order("t_id")->group("t_id")->select(); 
        //右边显示一半
        $rightMod       =$this->db->where($where)->order('id asc')->limit($num/2,$num)->select();
        foreach($rightMod as $k=>$v){
            $rightMod[$k]['desc_']   = explode(",",$rightMod[$k]['desc']);
        }
        $riIds          =array();
        foreach($rightMod as $v2){
            $riIds[]=$v2['id'];
        }
        $where2['t_id'] =array("in",implode(",",$riIds));
        $riDataList     =$this->db_data->where($where2)->order("t_id")->select();  
        //左边内容的一半资料        
        $this->assign("rMod",$rightMod);
        $this->assign("lMode",$leftMod);
        $this->assign("leDataList",$leDataList);
        $this->assign("riDataList",$riDataList);
        $this->assign("secList",$secList);
        $this->display();
    }
    
    /*
     * 文件下载列表
     * 
     * @#return #
     */
    public function down(){
        $ord['add_time']        =desc;
        if(I("request.mod_id")){
            $where['t_id']      =I("request.mod_id",'','code');            
        }
        if(I("request.sec_id")){
            $where['section_id']=I("request.sec_id",'','code');
        }
        $where['status']=1;
        $data           =$this->db_data->dataList($where,$ord,8,1);   
        $dataList       =$data['dataList'];
        $pageList       =$data['pageList'];
        $this->assign("pageList",$pageList);
        $this->assign("dataList",$dataList);
        $this->display();
    }
    
    /*
     * 资料下载
     * 
     * @return #
     */
    public function dataDownload(){
        $where=array(
            "id"    =>I("request.id","","code"),
            "status"=>1
        );
        if(I("request.t_id")){
            $where['t_id']  =I("request.t_id",'','code');            
        }
        if(I("request.sec_id")){
            $where['sec_id']=I("request.sec_id",'','code');
        }
        $testInfo   =$this->db_data->where($where)->find();
        $path       =$testInfo['file'];                             //路劲
        $tLoadName  ='Application/Public/Upload/'.$path;            //完整路劲
        $testName   =$testInfo['title'].'.'.pathinfo($path, PATHINFO_EXTENSION);
        if(!file_exists($tLoadName)){
            if(I("request.t_id","","code")&&I("request.sign",'','code')==1){
                $this->error("文件不存在",U("down",array("mod_id"=>I("request.t_id"))));
            }else if(I("request.t_id","","code")&&I("request.sign",'','code')==2){
                 $this->error("文件不存在",U('Index/index'));
            }else{
                $this->error("文件不存在",U("index"));
            }
        }
        $download=new Http();
        $download->download($tLoadName,$testName);
    }
}
